﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using HtmlAgilityPack;
using System.IO;
using ScormModel;

namespace WpfELearningAuthoringTool
{
    class ResourceUtil
    {

        public List<string> FetchLinksFromSource(string htmlSource)
        {
            List<string> imgScrs = new List<string>();
           HtmlDocument doc = new HtmlDocument();
            doc.LoadHtml(htmlSource);//or doc.Load(htmlFileStream)
            var nodes = doc.DocumentNode.SelectNodes(@"//img[@src]");
            if (nodes == null)
                return imgScrs;
            foreach (HtmlNode img in nodes)
            {
              HtmlAttribute att = img.Attributes["src"];
              imgScrs.Add(att.Value);
            }
            return imgScrs;
        }
        public List<string> FetchCSSFromSource(string htmlSource)
        {
            List<string> linkScrs = new List<string>();
            HtmlDocument doc = new HtmlDocument();
            doc.LoadHtml(htmlSource);//or doc.Load(htmlFileStream)
            var links = doc.DocumentNode.SelectNodes(@"//link[@href]");
            if (links == null)
                return linkScrs;
            foreach (HtmlNode link in links)
            {
                HtmlAttribute att = link.Attributes["href"];
                linkScrs.Add(att.Value);
            }
            return linkScrs;
        }
        public List<string> FetchJavaScriptFromSource(string htmlSource)
        {
            List<string> linkScrs = new List<string>();
            HtmlDocument doc = new HtmlDocument();
            doc.LoadHtml(htmlSource);//or doc.Load(htmlFileStream)
            var links = doc.DocumentNode.SelectNodes(@"//script[@src]");
            if (links == null)
                return linkScrs;
            foreach (HtmlNode link in links)
            {
                HtmlAttribute att = link.Attributes["src"];
                linkScrs.Add(att.Value);
            }
            return linkScrs;
        }
        public string  processImagePath(string htmlSource, string currentPath,Resource re)
        {
            List<string> list = FetchLinksFromSource(htmlSource);
            foreach (string m in list)
            {
                string iPath = m;
                if (iPath.Contains("http://"))
                    continue;
                else if (iPath.Contains("file:///"))
                {
                    iPath = iPath.Replace("file:///","");
                }
                string srcPath = iPath;
                string desName = System.IO.Path.GetFileName(srcPath);
                string desPath = currentPath + "/Images/" + desName;
                if (!File.Exists(desPath))
                {
                    File.Copy(srcPath, desPath, true);
                    FileRef fr = new FileRef();
                    fr.Bref = "Images/" + desName;
                    re.ListFileRef.Add(fr);
                }
            }
            foreach (string m in list)
            {
                string iPath = m;
                if (iPath.Contains("http://"))
                    continue;
                else if (iPath.Contains("file:///"))
                {
                    iPath = iPath.Replace("file:///", "");
                }
                string srcPath = iPath;
                string desName = System.IO.Path.GetFileName(srcPath);
                string desPath = currentPath + "/images/" + desName;
                if (srcPath != desPath)
                    htmlSource = htmlSource.Replace(m, desPath);
            }
            return htmlSource;
        }
        public string processCssPath(string htmlSource, string currentPath, Resource re) 
        {
            List<string> list = FetchLinksFromSource(htmlSource);
            foreach (string m in list)
            {
                string iPath = m;
                if (iPath.Contains("http://") || iPath.Contains("://"))
                    continue;
                string srcPath = iPath;
                string desName = System.IO.Path.GetFileName(srcPath);
                string desPath = currentPath + "/css/" + desName;
                if (!File.Exists(desPath))
                {
                    File.Copy(srcPath, desPath, true);
                    FileRef fr = new FileRef();
                    fr.Bref = "css/" + desName;
                    re.ListFileRef.Add(fr);
                }
            }
            foreach (string m in list)
            {
                string iPath = m;
                if (iPath.Contains("htp://") || iPath.Contains("://"))
                    continue;
                string srcPath = iPath;
                string desName = System.IO.Path.GetFileName(srcPath);
                string desPath = currentPath + "/css/" + desName;
                if (srcPath != desPath)
                    htmlSource = htmlSource.Replace(m, desPath);
            }
            return htmlSource;
        }
        public string processJavaScriptPath(string htmlSource, string currentPath, Resource re)
        {
            List<string> list = FetchLinksFromSource(htmlSource);
            foreach (string m in list)
            {
                string iPath = m;
                if (iPath.Contains("http://") || iPath.Contains("://"))
                    continue;
                string srcPath = iPath;
                string desName = System.IO.Path.GetFileName(srcPath);
                string desPath = currentPath + "/js/" + desName;
                if (!File.Exists(desPath))
                {
                    File.Copy(srcPath, desPath, true);
                    FileRef fr = new FileRef();
                    fr.Bref = "js/" + desName;
                    re.ListFileRef.Add(fr);
                }
            }
            foreach (string m in list)
            {
                string iPath = m;
                if (iPath.Contains("htp://") || iPath.Contains("://"))
                    continue;
                string srcPath = iPath;
                string desName = System.IO.Path.GetFileName(srcPath);
                string desPath = currentPath + "/js/" + desName;
                if (srcPath != desPath)
                    htmlSource = htmlSource.Replace(m, desPath);
            }
            return htmlSource;
        }

    }

}
